package com.hotelManager.www.service;

import com.hotelManager.www.dao.BrowseHistoryDao;
import com.hotelManager.www.po.BrowseHistory;
import com.hotelManager.www.util.JDBCUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

/**
 * 浏览历史服务类
 * 实现浏览历史相关功能
 */
public class BrowseHistoryService {
    private final BrowseHistoryDao browseHistoryDao;

    public BrowseHistoryService() {
        this.browseHistoryDao = new BrowseHistoryDao();
    }

    /**
     * 记录浏览历史
     * @param userId 用户ID
     * @param shopId 店铺ID
     * @return 记录是否成功
     */
    public boolean recordBrowseHistory(int userId, int shopId) {
        Connection conn = null;
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);

            // 创建浏览历史记录
            BrowseHistory history = new BrowseHistory();
            history.setUserId(userId);
            history.setShopId(shopId);
            history.setBrowseTime(new Timestamp(System.currentTimeMillis()));

            boolean success = browseHistoryDao.insert(conn, history);
            if (success) {
                conn.commit();
            } else {
                conn.rollback();
            }
            return success;
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.rollback();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
            return false;
        } finally {
            JDBCUtils.closeConnection(conn);
        }
    }

    /**
     * 获取用户浏览历史
     * @param userId 用户ID
     * @return 浏览历史列表
     */
    public List<BrowseHistory> getBrowseHistory(int userId) {
        Connection conn = null;
        try {
            conn = JDBCUtils.getConnection();
            return browseHistoryDao.getBrowseHistoryByUserId(conn, userId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            JDBCUtils.closeConnection(conn);
        }
    }

    /**
     * 清除用户浏览历史
     * @param userId 用户ID
     * @return 清除是否成功
     */
    public boolean clearBrowseHistory(int userId) {
        Connection conn = null;
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);

            boolean success = browseHistoryDao.deleteByUserId(conn, userId);
            if (success) {
                conn.commit();
            } else {
                conn.rollback();
            }
            return success;
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.rollback();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
            return false;
        } finally {
            JDBCUtils.closeConnection(conn);
        }
    }
} 